package com.cy.demo1.algorithm.miamiayyds.Function;

import com.cy.demo1.algorithm.miamiayyds.Class.*;
import com.cy.demo1.entity.Result1;

import java.io.IOException;
import java.util.ArrayList;
public class NBfunction 
{
    public Result1 getResult1(String str) throws IOException{
        /**
         * 朴素贝叶斯训练
         */
        ArrayList<String[]> csvList_train = new ArrayList<String[]>();
        ArrayList<String[]> csvList_test = new ArrayList<String[]>();

        ArrayList<String> type_train = new ArrayList<String>();
        ArrayList<String> type_test = new ArrayList<String>();
        ArrayList<String> type_result = new ArrayList<String>();

        IO csvio = new IO();
        csvList_train = csvio.readCsv("C:\\Users\\tanxy\\Desktop\\csv\\JDT.csv");
        csvList_test = csvio.readCsv(str);

         /*
        转换格式
         */
        change change = new change();
        double[][] dataList_train = change.exchange(csvList_train,type_train);
        double[][] dataList_test = change.exchange(csvList_test,type_test);

        //clean为0，buggy为1
        double[] train_type_double = change.type_change(type_train);
        double[] test_type_double = change.type_change(type_test);

        //离散化
        dataList_train = Standardization.discrete(dataList_train);
        dataList_test = Standardization.discrete(dataList_test);

        NaiveBayesNode nb = new NaiveBayesNode();
        nb.train(dataList_train,train_type_double);
        nb.test(dataList_test,test_type_double);
        type_result = nb.getResult();

        //计算得分
        Judge judge = new Judge();
        judge.score(type_test,type_result);
        //judge.roc();
        //judge.snapshot();


        //写入csv
        //csvio.writeCsv("D:\\csvdata\\NBresult.csv",type_result);

        Result1 result1 = new Result1();
        result1.setAcc(judge.acc);
        result1.setPre(judge.pre);
        result1.setRec(judge.rec);
        result1.setF1(judge.f1);
        result1.setAuc(judge.auc);

        String rocx = String.valueOf(judge.f[0][1]);
        String rocy = String.valueOf(judge.f[0][0]);
        for(int i = 1;i<10;i++){
            rocx = rocx + "," + String.valueOf(judge.f[i][1]);
            rocy = rocy + "," + String.valueOf(judge.f[i][0]);
        }
        result1.rocx = rocx;
        result1.rocy = rocy;

        System.out.println("rox: " + result1.getRocx());
        System.out.println("roy: " + result1.getRocy());
        return result1;
    }
}